package zucc.lcj.android_finalwork.me;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import zucc.lcj.android_finalwork.LoginActivity;
import zucc.lcj.android_finalwork.MyDatabaseHelper;
import zucc.lcj.android_finalwork.R;

public class SetupActivity extends AppCompatActivity {
    private MyDatabaseHelper dbHelper;
    Button exit;
    Button change_pwd;
    Button change_age;
    Button header;
    Button back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_more4);
        dbHelper = new MyDatabaseHelper(this,"UserStore.db",null,1);

        header = findViewById(R.id.me_more4_userbtm);
        header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(SetupActivity.this, HeaderActivity.class);
                startActivity(intent);
            }
        });

        back = findViewById(R.id.me_more4_backbtm);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(SetupActivity.this, MeActivity.class);
                startActivity(intent);
            }
        });



        exit = findViewById(R.id.me_more4_exitbtm);
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SetupActivity.this);
                builder.setTitle("提示")
                        .setMessage("确定退出登录吗？")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent();
                        intent.setClass(SetupActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }
                });
                builder.show();
            }
        });

        change_pwd = findViewById(R.id.me_more4_pwdbtm);
        change_pwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(SetupActivity.this, ChangePwdActivity.class);
                startActivity(intent);
            }
        });

        change_age = findViewById(R.id.me_more4_agebtm);
        change_age.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("Range")
            @Override
            public void onClick(View view) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                int age=0;
                Cursor cursor = db.rawQuery("select * from UserInfo where user_name=?",new String[]{LoginActivity.global_login_user});
                if (cursor.moveToFirst()){
                    age = cursor.getInt(cursor.getColumnIndex("user_age"));
                }
                final EditText inputServer = new EditText(SetupActivity.this);
                inputServer.setHint(String.valueOf(age));
                AlertDialog.Builder builder = new AlertDialog.Builder(SetupActivity.this);
                builder.setTitle("更改您的年龄").setView(inputServer)
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        String ages = inputServer.getText().toString();
                        System.out.println(ages);
                        String sql="update UserInfo set user_age="+Integer.valueOf(ages)+" where UserInfo.user_name='"+LoginActivity.global_login_user+"'";
                        db.execSQL(sql);
                        Toast.makeText(getApplicationContext(), "修改成功", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.show();
            }
        });

    }
}